package com.oracle.mall.controller;

import com.alibaba.fastjson.JSON;
import com.oracle.mall.po.CartDetail;
import com.oracle.mall.po.Member;
import com.oracle.mall.po.Nav;
import com.oracle.mall.po.Product;
import com.oracle.mall.service.CartService;
import com.oracle.mall.service.NavService;
import com.oracle.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ProductController {

    @Autowired
    private CartService cartService;

    @Autowired
    private ProductService productService;

    @Autowired
    private NavService navService;

    @RequestMapping("/product/{pid}")
    public String product(Model model, @PathVariable Integer pid, HttpSession session){
        Product product = productService.queryByPid(pid);
        model.addAttribute("product",product);

        List<Nav> nav = navService.getNav();
        model.addAttribute("nav",nav);

        if(session.getAttribute("member")!=null){
            Member member = (Member) session.getAttribute("member");
            Integer mid = member.getMid();
            CartDetail cartDetail = cartService.queryTotal(mid);
            model.addAttribute("cartDetail",cartDetail);
        }
        return "product";
    }
}
